package com.zfx.service.impl;

import com.github.pagehelper.PageHelper;
import com.zfx.mapper.IUserMapper;
import com.zfx.pojo.User;
import com.zfx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 用户服务层接口实现类
 * @author: zfx
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Transactional(readOnly = true)
    @Override
    public List<User> findByUser(User user, int page, int size) {
        PageHelper.startPage(page, size);
        return userMapper.select(user);
    }

    @Transactional(readOnly = true)
    @Override
    public User findById(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User login(User user) {
        return userMapper.selectOne(user);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean register(User user) {
        User u = new User();
        u.setUsername(user.getUsername());
        if(userMapper.select(u).size() == 0) {
            userMapper.insert(user);
            return true;
        }
        return false;
    }
}
